package com.trippin.servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityNotFoundException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.trippin.DB.DBHandler;
import com.trippin.DB.entities.AttractionEntity;
import com.trippin.DB.entities.ScheduledAttractionEntity;
import com.trippin.Utils.ServletUtil;
import com.trippin.Utils.ServletUtilConstants;

@SuppressWarnings("serial")
public class ScheduleAttractionServlet extends HttpServlet {

	private static final Logger logger = Logger.getLogger(ScheduleAttractionServlet.class.getCanonicalName());
	
	private static DBHandler dbHandlerInstance = DBHandler.INSTANCE;

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, EntityNotFoundException, IOException  {

		resp.setContentType("application/json");

		Action action = ServletUtil.getActionFromHeader(req,resp,ScheduleAttractionServlet.class.toString());

		long attId = ServletUtil.getLongFromHeader(req, resp, ServletUtilConstants.ATTRACTION_ID,
				ScheduleAttractionServlet.class.toString(), action.getName());
		long userId = ServletUtil.getLongFromHeader(req, resp, ServletUtilConstants.USER_ID,
				ScheduleAttractionServlet.class.toString(), action.getName());		
		long tripId = ServletUtil.getLongFromHeader(req, resp, ServletUtilConstants.TRIP_ID,
				TripServlet.class.toString(), action.getName());

		String bodyResponse = "";

		long scheduleAttrId = -1;

		switch(action) {
		case ATTEND_TO_SCHEDULE_ATTRACTION:
		case UNATTEND_FROM_SCHEDULE_ATTRACTION:
		case ADD_PLUS_TO_SCHEDULE_ATTRACTION:
		case ADD_MINUS_TO_SCHEDULE_ATTRACTION:	
			scheduleAttrId = ServletUtil.getLongFromHeader(req, resp, ServletUtilConstants.SCHEDULE_ATTRACTION_ID,
					ScheduleAttractionServlet.class.toString(), action.getName());
		}

		try {
			switch(action) {
			case ATTEND_TO_SCHEDULE_ATTRACTION:
				//returns the schedule attraction
				bodyResponse = attendToScheduleAttraction(userId, tripId, attId, scheduleAttrId);
				break;

			case UNATTEND_FROM_SCHEDULE_ATTRACTION:
				//returns the schedule attraction
				bodyResponse = unattendFromScheduleAttraction(userId, tripId, attId, scheduleAttrId);
				break;

			case CREATE_SCHEDULE_ATTRACTION:
				bodyResponse = createScheduleAttraction(userId, tripId, attId, req);
				break;
				
			case ADD_PLUS_TO_SCHEDULE_ATTRACTION:
				bodyResponse = addPlusOrMinus(userId, scheduleAttrId, true);
				break;
				
			case ADD_MINUS_TO_SCHEDULE_ATTRACTION:
				bodyResponse = addPlusOrMinus(userId, scheduleAttrId, false);
				break;

			default:
				ServletUtil.printError(resp,action.getName(), AttractionServlet.class.getName(), ServletUtilConstants.ERROR_WRONG_ACTION_NAME);
				break;
			}

			ServletUtil.createResponse(resp, action.getName(), bodyResponse);
		
		}catch(Exception e) {
			ServletUtil.printError(resp,action.getName(), ScheduleAttractionServlet.class.getName(), e.getMessage());
			logger.log(Level.SEVERE,e.toString(), e);;
		}

	}

	private String addPlusOrMinus(long userId, long scheduleAttId, boolean addPlus) {
		ScheduledAttractionEntity scheduleAttraction = dbHandlerInstance.addPlusOrMinusToScheduleAttraction(userId, scheduleAttId, addPlus);
		return ServletUtil.getResponseBody(scheduleAttraction);
	}

	private String createScheduleAttraction(long userId, long tripId, long attrId, 
			HttpServletRequest req) throws IOException, EntityNotFoundException {
		BufferedReader in = req.getReader();
		String reqBody = ServletUtil.getRequestBody(in);
		in.close();
		ScheduledAttractionEntity scheduleAttraction = (ScheduledAttractionEntity)ServletUtil.getFromJSON(reqBody, ScheduledAttractionEntity.class);
		AttractionEntity entity = dbHandlerInstance.createNewScheduleAttraction(userId, tripId, attrId, scheduleAttraction.getStartTime());
		return ServletUtil.getResponseBody(entity);
	}

	private String unattendFromScheduleAttraction(long userId, 
			long tripId, long attId, long scheduleAtrId) throws IOException {
		AttractionEntity scheduleAttractions = 
				dbHandlerInstance.unAttendFromScheduleAttraction(userId,tripId,attId,scheduleAtrId);
		return ServletUtil.getResponseBody(scheduleAttractions);
	}

	private String attendToScheduleAttraction(long userId, long tripId, long attId, long scheduleAtrId
			) throws IOException {
		AttractionEntity scheduleAttractions = 
				dbHandlerInstance.attendToScheduleAttraction(userId,tripId,attId,scheduleAtrId);
		return ServletUtil.getResponseBody(scheduleAttractions);	
	}
}
